<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create the output for the static-pages
 * get: page/tpl.php
 * used from core.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
//$refPage->assign('termsNotAccept', '');
// if call from adress form
if (isset($_POST['func']))
{
	if ($_POST['func'] === 'createGuest')
	{
		$pagefile = 'module/cart/cart.saveadress.php';
		if (is_file($pagefile))
			include_once($pagefile);
	}
}

if (checkUser() === FALSE and ! isset($_SESSION['ordererIdx']))
{ // if user not logged-in or no guest-id
	$pagefile = 'module/cart/cart.ordereradress.php';
	if (is_file($pagefile))
		include_once($pagefile);
}
elseif (!isset($_SESSION['ordererIdx']))
{ // Copy Customerdata to orderer-table
	db::getInstance()->where('customer_name', $_SESSION['customernamex']);
	$customer = db::getInstance()->getOne('customer');

	$mysqlBuffer = array();
	if (!empty($customer['customer_company']))
	{
		$mysqlBuffer['orderer_company'] = $customer['customer_company'];
	}

	if (!empty($customer['customer_firstname']))
	{
		$mysqlBuffer['orderer_firstname'] = $customer['customer_firstname'];
	}

	if (!empty($customer['customer_lastname']))
	{
		$mysqlBuffer['orderer_lastname'] = $customer['customer_lastname'];
	}

	if (!empty($customer['customer_street']))
	{
		$mysqlBuffer['orderer_street'] = $customer['customer_street'];
	}

	if (!empty($customer['customer_zip']))
	{
		$mysqlBuffer['orderer_zip'] = $customer['customer_zip'];
	}

	if (!empty($customer['customer_city']))
	{
		$mysqlBuffer['orderer_city'] = $customer['customer_city'];
	}

	if (!empty($customer['customer_country']))
	{
		$mysqlBuffer['orderer_country'] = $customer['customer_country'];
	}

	if (!empty($customer['customer_state']))
	{
		$mysqlBuffer['orderer_state'] = $customer['customer_state'];
	}

	if (!empty($customer['customer_phone']))
	{
		$mysqlBuffer['orderer_phone'] = $customer['customer_phone'];
	}

	if (!empty($customer['customer_fax']))
	{
		$query1 .= ", orderer_fax";
		$query2 .= "', '" . $customer['customer_fax'];
		$mysqlBuffer['orderer_company'] = $customer['customer_company'];
	}

	if (!empty($customer->customer_handy))
	{
		$mysqlBuffer['orderer_handy'] = $customer['customer_handy'];
	}

	if (!empty($customer['customer_email']))
	{
		$mysqlBuffer['orderer_email'] = $customer['customer_email'];
	}

	if (!empty($customer['customer_level']))
	{
		$mysqlBuffer['orderer_level'] = $customer['customer_level'];
	}

	if (!empty($customer['customer_id']))
	{
		$mysqlBuffer['orderer_customer_id'] = $customer['customer_id'];
	}

	$mysqlBuffer['orderer_comment'] = 'New Guest';

	db::getInstance()->insert('orderer', $mysqlBuffer);
	$mysqlBuffer = NULL;

	db::getInstance()->where('orderer_comment', 'New Guest');
	$result = db::getInstance()->getOne('orderer');
	$_SESSION['ordererIdx'] = $result['orderer_id'];

	$data = Array('orderer_comment' => ' ');
	db::getInstance()->where('orderer_comment', 'New Guest');
	db::getInstance()->update('orderer', $data);

	// TODO Adresse von Registrierten Käufern anzeigen und vorher prüfen
	// mit ok-Fenster! Ähnlich für Gast-Käufer.
}

if (isset($_SESSION['ordererIdx']))
{ //Create data for order if adress ok
	$pagefile = 'module/function/price.calculate.php';
	if (is_file($pagefile))
		include_once($pagefile);

// Orderadress 
	$refPage->assign('orderadress', _('Orderadress'));
	db::getInstance()->where('orderer_id', $_SESSION['ordererIdx']);
	$result = db::getInstance()->getOne('orderer');

	$refPage->assign('company', $result['orderer_company']);
	$refPage->assign('name', $result['orderer_firstname'] . ' ' . $result['orderer_lastname']);
	$refPage->assign('street', $result['orderer_street']);
	$refPage->assign('city', $result['orderer_zip'] . ' ' . $result['orderer_city']);

// productlist
	$cart = $refCart->getProducts();
	$price = '';
	$tax = array();
	$cartx = array();
	$pricex = 0;

	$refCart->addToTotalPrice(0, TRUE);

	foreach ($cart as $product)
	{
		$pricePerItem = $refCart->productPricePerItem($product['cart_product_id'],
			$product['number'], $product);
		$priceTotal = $refCart->productPrice($product['cart_product_id'], $product['number'],
			$product);
		$refCart->addToTotalPrice($priceTotal, $product['cart_product_id']);

		$tax = $refCart->getTax($priceTotal, $product['cart_product_id']);

		$orderx[] = array(
		 'sku' => $product['sku'],
		 'product' => ($product['product'] . $refCart->optionsText2Html($product['optionsText'])),
		 'pricePerItem' => $pricePerItem,
		 'priceTotal' => $priceTotal,
		 'tax' => $refCart->getTax($priceTotal, $product['cart_product_id'], FALSE),
		 'number' => $product['number']);

//TODO include produkt-details
	}
	$refPage->assign('productList', $orderx);
	$_SESSION['productListx'] = $orderx;

	// prices
	$refPage->assign('price', $refCart->getTotalPrice(TRUE));
	$refPage->assign('tax', $refCart->getTotalPriceTax(TRUE));

	// shipping
	$refPage->assign('shipping', _('Shipping Method'));
	db::getInstance()->where('shipping_active', '1');
	db::getInstance()->orderBy('shipping_order', "ASC");
	$shipping = db::getInstance()->get('shipping');

	$shippingx = '';
	foreach ($shipping as $method)
	{
		$pagefile = 'plugins/shipping/' . $method['shipping_file'] . '.shipping.php';
		$sFunction = $method['shipping_file'];

		if (is_file($pagefile))
		{
			include_once($pagefile);

			$shippingx .= '<input type="radio" name="ordershipping" value="' .
				$method['shipping_name'] . '" ';
			if ($method['shipping_order'] == '1')
			{
				$shippingx .= 'checked="checked" />  ' . $method['shipping_name'];
			}
			else
			{
				$shippingx .= '/>  ' . $method['shipping_name'];
			}
			$addCost = $sFunction($cart, $method['shipping_value'], $method['shipping_table']);
			if ($addCost <> 0)
				$shippingx .= '<b>' . $refCart->formatToPrice($addCost) . '</b>';
			$shippingx .= '<br>';
		}
	}
	$refPage->assign('ordershipping', $shippingx);

// payment
	$refPage->assign('payment', _('Payment Method'));
	db::getInstance()->where('payment_active', '1');
	db::getInstance()->orderBy('payment_order', "ASC");
	$payment = db::getInstance()->get('payment');

	$paymentx = '';
	foreach ($payment as $method)
	{
		$pagefile = 'plugins/payment/' . $method['payment_file'] . '.payment.php';
		$sFunction = $method['payment_file'];

		if (is_file($pagefile))
		{
			include_once($pagefile);

			$paymentx .= '<input type="radio" name="orderpayment" value="' .
				$method['payment_name'] . '" ';
			if ($method['payment_order'] == '1')
			{
				$paymentx .= 'checked="checked" />  ' . $method['payment_name'] . '<br>';
			}
			else
			{
				$paymentx .= '/>  ' . $method['payment_name'] . '<br>';
			}
		}
	}
	$refPage->assign('orderpayment', $paymentx);

	// comment
	$refPage->assign('comment', _('Your comment'));
	if (!isset($_SESSION['commentx']))
		$_SESSION['commentx'] = '';
	$refPage->assign('ordercomment',
		'<textarea id="ordercommentx" name="ordercommentx" rows="8" cols="56">' .
		$_SESSION['commentx'] . '</textarea>');

	$refPage->assign('formstart',
		'<form action = "' . URL . '?page=cart&title=order' .
		'&name=' . _('Shoppingcart') . '&cart=confirm" method = "post">');
	$refPage->assign('formend', '</form>');

	$refPage->assign('order_button',
		'<input class="btn_order" name="btn_order" title="' . _('Buy now') .
		'" src="template/images/btn_order.png" alt="' . _('Buy now') .
		'" type="image">');
	$refPage->assign('back_button',
		'<a title="' . _('Back') . '" href="/index.php?page=cart&title=show&name=Warenkorb">' .
		'<img src="/template/images/btn_back.png" alt = "' .
		_('Back') . '" titel = "Warenkorb"> </a>');
	pagepointer::setCurrentPage();
	$refPage->addtpl('content', '/' . $page . '/' . $title . '.tpl.php');
}
?>